#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "config.h"
#include "heroplane.h"
#include<QIcon>
#include<QPainter>
#include<QMouseEvent>
#include<ctime>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    //调用初始化场景
    initScene();

    //启动游戏
    playGAME();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::initScene()
{
    //设置窗口固定尺寸
    setFixedSize(GAME_WIDTH,GAME_HEIGHT);
    //设置标题
    setWindowTitle(GAME_TITLE);
    //加载标题图标
    setWindowIcon(QIcon(":/res/enemy1"));

    //定时器初始化
    m_timer.setInterval(GAME_RATE);

    //敌机出场时间间隔初始化
    m_recorder=0;

    //随机数种子
    srand((unsigned int)time(NULL));

}

void MainWindow::playGAME()
{
    //启动定时器
    m_timer.start();
    //监听定时器信号
    connect(&m_timer,&QTimer::timeout,[=](){
        //敌机出场
        enemyToScene();
        //更新游戏坐标
        updatePosition();
        //绘制到屏幕中
        update();
        //击中检测
        collisionDetection();
    });
}

void MainWindow::updatePosition()
{
    //更新地图坐标
    m_map.mapPosition();

    //发射子弹
    m_hero.shoot();

    //计算飞行子弹坐标
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets[i].m_Free==false){
            m_hero.m_bullets[i].updatePosition();
        }
    }
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets1[i].m_Free==false){
            m_hero.m_bullets1[i].updatePosition();
        }
    }

    //敌机出场
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemies[i].m_Free==false){
            m_enemies[i].updatePosition();
        }
    }
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter painter(this);

    //绘制地图
    painter.drawPixmap(0,m_map.m_map1_posY,m_map.m_map1);
    painter.drawPixmap(0,m_map.m_map2_posY,m_map.m_map2);

    //绘制己方飞机
    painter.drawPixmap(m_hero.m_X,m_hero.m_Y,m_hero.m_Plane);

    //绘制子弹
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets[i].m_Free==false){
            painter.drawPixmap(m_hero.m_bullets[i].m_X,m_hero.m_bullets[i].m_Y,m_hero.m_bullets[i].m_Bullet);
        }
    }
    for(int i=0;i<BULLET_NUM;i++){
        if(m_hero.m_bullets1[i].m_Free==false){
            painter.drawPixmap(m_hero.m_bullets1[i].m_X,m_hero.m_bullets1[i].m_Y,m_hero.m_bullets1[i].m_Bullet);
        }
    }

    //绘制敌机
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemies[i].m_Free==false){
            painter.drawPixmap(m_enemies[i].m_X,m_enemies[i].m_Y,m_enemies[i].m_enemy);
        }
    }
}

void MainWindow::mouseMoveEvent(QMouseEvent *event)
{
    //鼠标事件捕捉移动位置
    int x=event->x()-m_hero.m_Rect.width()*0.5;
    int y=event->y()-m_hero.m_Rect.height()*0.5;

    //边界控制
    if(x<=0){
        x=0;
    }
    if(x>=GAME_WIDTH-m_hero.m_Rect.width()){
        x=GAME_WIDTH-m_hero.m_Rect.width();
    }
    if(y<=0){
        y=0;
    }
    if(y>=GAME_HEIGHT-m_hero.m_Rect.height()){
        y=GAME_HEIGHT-m_hero.m_Rect.height();
    }

    //移动飞机位置
    m_hero.setPosition(x,y);
}

void MainWindow::collisionDetection()
{
    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemies[i].m_Free){
            continue;
        }

        for(int j=0;j<BULLET_NUM;j++){
            if(m_hero.m_bullets[j].m_Free){
                continue;
            }

            //左是否击中敌机
            if(m_enemies[i].m_Rect.intersects(m_hero.m_bullets[j].m_Rect)){
                m_enemies[i].m_Free=true;
                m_hero.m_bullets[j].m_Free=true;
            }
        }

        for(int j=0;j<BULLET_NUM;j++){
            if(m_hero.m_bullets1[j].m_Free){
                continue;
            }

            //右是否击中敌机
            if(m_enemies[i].m_Rect.intersects(m_hero.m_bullets1[j].m_Rect)){
                m_enemies[i].m_Free=true;
                m_hero.m_bullets1[j].m_Free=true;
            }
        }
    }
}

void MainWindow::enemyToScene()
{
    m_recorder++;
    if(m_recorder<ENEMY_SPACE){
        return;
    }

    m_recorder=0;

    for(int i=0;i<ENEMY_NUM;i++){
        if(m_enemies[i].m_Free){
            m_enemies[i].m_Free=false;
            m_enemies[i].m_X=rand()%(GAME_WIDTH-m_enemies[i].m_Rect.width());
            m_enemies[i].m_Y=-m_enemies[i].m_Rect.height();
            break;
        }
    }
}

